
/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * Company:      <p>
 * @author
 * @version 1.0
 */
package com.timber.login;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;

import pv.jfcx.*;

import com.timber.util.*;
import com.borland.jbcl.layout.*;
import javax.swing.event.*;



public class ChangePwdDialog extends JDialog
{


  public ChangePwdDialog()
  {
    try {
      jbInit();
      setSize(380, 170);
      pack();
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      int screenHeight = d.height;
      int screenWidth = d.width;
      int myWidth = getWidth();
      int myHeight = getHeight();
      setLocation( ( screenWidth - myWidth ) / 2,
		   ( screenHeight - myHeight ) / 2 );

    }
      catch(Exception e) {e.printStackTrace();}

  }

  public static final int CHANGE_PWD	= 1;
  public static final int CANCEL	= 2;

  protected JPanel pParent;
  JPanel pUser = new JPanel();

  //JPanel pUser = new JPanel();

  String newPassword;
  //LoginManager loginManager = new LoginManager();
  Border border1;
  Border border2;
  JPasswordField tVerify = new JPasswordField();
  JPasswordField tNewPassword = new JPasswordField();
  JLabel lVerify = new JLabel();
  JLabel lNewPassword = new JLabel();
  JPanel actionP = new JPanel();
  JButton btnLogon = new JButton();
  //String password = "";
  private int actionCode;
  protected FlowLayout flowLayout1 = new FlowLayout();
  protected JButton btnCancel = new JButton();
  protected Border border3;
  protected Border border4;
  protected GridBagLayout gridBagLayout1 = new GridBagLayout();
  protected FlowLayout flowLayout2 = new FlowLayout();
  protected Border border5;


  private void jbInit() throws Exception
  {

    this.getRootPane().setDefaultButton(btnLogon);
    pParent = (JPanel)this.getContentPane();
    border1 = BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134));
    border2 = BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),BorderFactory.createEmptyBorder(5,5,5,5));
    border3 = BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134));
    border4 = BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142));
    border5 = BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134));
    pParent.setAlignmentX((float) 0.0);
    pParent.setAlignmentY((float) 0.0);
    pParent.setBorder(border5);
    pParent.setMinimumSize(new Dimension(300, 610));
    pParent.setPreferredSize(new Dimension(290, 130));
    //pParent.setPreferredSize(new Dimension(450, 210));
    pParent.setLayout(flowLayout2);
    //pUser.setPreferredSize(new Dimension(350, 200));
    //pUser.setLayout(flowLayout1);
    this.setTitle("Change Password");
    pUser.setPreferredSize(new Dimension(270, 80));
    pUser.setLayout(gridBagLayout1);
    tVerify.setPreferredSize(new Dimension(140, 21));
    tNewPassword.setPreferredSize(new Dimension(140, 21));
    lVerify.setPreferredSize(new Dimension(130, 17));
    lVerify.setToolTipText("");
    lVerify.setMaximumSize(new Dimension(789, 17));
    lVerify.setFont(new java.awt.Font("Dialog", 1, 11));
    lVerify.setText("Verify New Password");
    lNewPassword.setText("New Password");
    lNewPassword.setPreferredSize(new Dimension(130, 17));
    lNewPassword.setMaximumSize(new Dimension(640, 17));
    lNewPassword.setFont(new java.awt.Font("Dialog", 1, 11));

    btnLogon.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        btnLogon_actionPerformed(e);
      }
    });
    btnLogon.setFont(new java.awt.Font("Dialog", 1, 11));
    btnLogon.setPreferredSize(new Dimension(49, 27));
    btnLogon.setText("Ok");
    //pUser.setPreferredSize(new Dimension(380, 160));
    btnCancel.setFont(new java.awt.Font("Dialog", 1, 11));
    btnCancel.setPreferredSize(new Dimension(71, 27));
    btnCancel.setText("Cancel");
    btnCancel.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnCancel_actionPerformed(e);
      }
    });
    //pUser.add(pUserName, null);
    actionP.setPreferredSize(new Dimension(270, 37));
    flowLayout2.setVgap(1);
    pParent.add(pUser, null);
    pUser.add(lNewPassword, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    pUser.add(tNewPassword, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pUser.add(lVerify, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    pUser.add(tVerify, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pParent.add(actionP, null);
    actionP.add(btnLogon, null);
    actionP.add(btnCancel, null);


  }




  public int getActionCode() {return actionCode; }
  public String getNewPassword(){return newPassword;}




   protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING)
    {
      actionCode = this.CANCEL;
      setVisible( false );
    }
  }

  void btnLogon_actionPerformed(ActionEvent e)
  {
    actionCode = this.CHANGE_PWD;
    if(!validateLogin()) return;
    setVisible( false );
  }

  void btnCancel_actionPerformed(ActionEvent e)
  {
    actionCode = this.CANCEL;
    setVisible( false );
  }

  private boolean validateLogin()
  {
    char [] arr = tNewPassword.getPassword();
    char [] arr1 = tVerify.getPassword();
    newPassword = "";
    String verify = "";

    /***** Make sure fields are not blank ***************/
    if (arr == null || arr.length < 1)
    {
      JOptionPane.showMessageDialog(this, "Please enter new password");
      return false;
    }
    if (arr1 == null || arr1.length < 1)
    {
      JOptionPane.showMessageDialog(this, "Please verify new password");
      return false;
    }
    /*******************************************************/

    for (int i=0; i<arr.length; i++)
      newPassword = newPassword + arr[i];
    for (int i=0; i<arr1.length; i++)
      verify = verify + arr1[i];

    if(!newPassword.equals(verify))
    {
      JOptionPane.showMessageDialog(this, "'New Password' and 'Verify New Password' fields do not match");
      return false;
    }
    return true;
  }


}
